/**
 * 作者: wgx
 * 日期: 2024-11-26 17:10:20
 * 备注: 用户商店限购信息
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserShopLimit;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserShopLimitModel extends BaseModel implements Model {
    public static final String LISTKEY = "user_id.shop_type";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // id
    protected long userId; // 用户Id
    protected int shopType; // 商店类型
    protected String dailyBuy = ""; // 每日限购记录
    protected long dailyTime; // 每日限购的重置时间
    protected String weekBuy = ""; // 每周限购记录
    protected long weekTime; // 每周限购的重置时间
    protected String monthBuy = ""; // 每月限购记录
    protected long monthTime; // 每月限购的重置时间
    protected String totalBuy = ""; // 终身限购

    public void reset() {
    }

    protected String getKey() {
        return "UserShopLimit." + userId + "." + shopType;
    }

    protected String getListKey() {
        return "UserShopLimit." + userId;
    }

    protected String getListItemKey() {
        return shopType + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_shop_limit"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, userId, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, userId, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // id
        obj.put("userId", userId); // 用户Id
        obj.put("shopType", shopType); // 商店类型
        obj.put("dailyBuy", dailyBuy); // 每日限购记录
        obj.put("dailyTime", dailyTime); // 每日限购的重置时间
        obj.put("weekBuy", weekBuy); // 每周限购记录
        obj.put("weekTime", weekTime); // 每周限购的重置时间
        obj.put("monthBuy", monthBuy); // 每月限购记录
        obj.put("monthTime", monthTime); // 每月限购的重置时间
        obj.put("totalBuy", totalBuy); // 终身限购
        return obj;
    }

    public PBUserShopLimit.Builder takeInitBuilder() {
        PBUserShopLimit.Builder sb = PBUserShopLimit.newBuilder();
        if (shopType != 0) {
            sb.setShopType(shopType);
        }
        if (!dailyBuy.isEmpty()) {
            sb.setDailyBuy(dailyBuy);
        }
        if (dailyTime != 0) {
            sb.setDailyTime(dailyTime);
        }
        if (!weekBuy.isEmpty()) {
            sb.setWeekBuy(weekBuy);
        }
        if (weekTime != 0) {
            sb.setWeekTime(weekTime);
        }
        if (!monthBuy.isEmpty()) {
            sb.setMonthBuy(monthBuy);
        }
        if (monthTime != 0) {
            sb.setMonthTime(monthTime);
        }
        if (!totalBuy.isEmpty()) {
            sb.setTotalBuy(totalBuy);
        }
        return sb;
    }

    public PBUserShopLimit.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserShopLimit.Builder sb = PBUserShopLimit.newBuilder();
        if (isFieldUpdate("shopType")) {
            sb.setShopType(shopType);
        }
        if (isFieldUpdate("dailyBuy")) {
            sb.setDailyBuy(dailyBuy);
        }
        if (isFieldUpdate("dailyTime")) {
            sb.setDailyTime(dailyTime);
        }
        if (isFieldUpdate("weekBuy")) {
            sb.setWeekBuy(weekBuy);
        }
        if (isFieldUpdate("weekTime")) {
            sb.setWeekTime(weekTime);
        }
        if (isFieldUpdate("monthBuy")) {
            sb.setMonthBuy(monthBuy);
        }
        if (isFieldUpdate("monthTime")) {
            sb.setMonthTime(monthTime);
        }
        if (isFieldUpdate("totalBuy")) {
            sb.setTotalBuy(totalBuy);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public int getShopType() {
        return shopType;
    }

    public void putShopType(int shopType) {
        if (this.shopType != shopType) {
            this.shopType = shopType;
            updateRedisHashField(getKey(), "shopType", this.shopType);
        }
    }

    public void addShopType(int value) {
        this.shopType += value;
        updateRedisHashField(getKey(), "shopType", this.shopType);
    }

    public String getDailyBuy() {
        return dailyBuy;
    }

    public void putDailyBuy(String dailyBuy) {
        if (!this.dailyBuy.equals(dailyBuy)) {
            this.dailyBuy = dailyBuy;
            updateRedisHashField(getKey(), "dailyBuy", this.dailyBuy);
        }
    }

    public long getDailyTime() {
        return dailyTime;
    }

    public void putDailyTime(long dailyTime) {
        if (this.dailyTime != dailyTime) {
            this.dailyTime = dailyTime;
            updateRedisHashField(getKey(), "dailyTime", this.dailyTime);
        }
    }

    public void addDailyTime(long value) {
        this.dailyTime += value;
        updateRedisHashField(getKey(), "dailyTime", this.dailyTime);
    }

    public String getWeekBuy() {
        return weekBuy;
    }

    public void putWeekBuy(String weekBuy) {
        if (!this.weekBuy.equals(weekBuy)) {
            this.weekBuy = weekBuy;
            updateRedisHashField(getKey(), "weekBuy", this.weekBuy);
        }
    }

    public long getWeekTime() {
        return weekTime;
    }

    public void putWeekTime(long weekTime) {
        if (this.weekTime != weekTime) {
            this.weekTime = weekTime;
            updateRedisHashField(getKey(), "weekTime", this.weekTime);
        }
    }

    public void addWeekTime(long value) {
        this.weekTime += value;
        updateRedisHashField(getKey(), "weekTime", this.weekTime);
    }

    public String getMonthBuy() {
        return monthBuy;
    }

    public void putMonthBuy(String monthBuy) {
        if (!this.monthBuy.equals(monthBuy)) {
            this.monthBuy = monthBuy;
            updateRedisHashField(getKey(), "monthBuy", this.monthBuy);
        }
    }

    public long getMonthTime() {
        return monthTime;
    }

    public void putMonthTime(long monthTime) {
        if (this.monthTime != monthTime) {
            this.monthTime = monthTime;
            updateRedisHashField(getKey(), "monthTime", this.monthTime);
        }
    }

    public void addMonthTime(long value) {
        this.monthTime += value;
        updateRedisHashField(getKey(), "monthTime", this.monthTime);
    }

    public String getTotalBuy() {
        return totalBuy;
    }

    public void putTotalBuy(String totalBuy) {
        if (!this.totalBuy.equals(totalBuy)) {
            this.totalBuy = totalBuy;
            updateRedisHashField(getKey(), "totalBuy", this.totalBuy);
        }
    }

}